#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# Created by Admin on 2024/7/11
# All rights reserved.
#
from fontTools.ttLib import TTFont

def decode_text_with_font(font_path, encoded_text, cmap_table_index=0):
    font = TTFont(font_path)
    cmap = font['cmap'].tables[cmap_table_index]

    # 创建一个字典，将字形索引映射到Unicode字符
    glyph_to_char = {glyphID: code for code, glyphID in cmap.cmap.items()}

    # 将编码文本中的每个字符替换为对应的Unicode字符
    decoded_text = ''.join(glyph_to_char.get(ord(char), '') for char in encoded_text)

    font.close()
    return decoded_text

# TTF字体文件路径
font_path = 'read.ttf'
# 编码的文本
encoded_text = "在希波战争结束的一半世纪，..."

# 解码文本
decoded_text = decode_text_with_font(font_path, encoded_text)
print(decoded_text)